<?php


namespace app\zhongcan\controller;


use api\web\model\NavModel;
use api\web\model\NewsModel;
use app\zhongcan\validate\NewsVaildate;
use cmf\controller\AdminBaseController;



class NewsController extends AdminBaseController
{
    public function index()
    {
        $key = input('key');
        $where = [];
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        $list = NewsModel::order('list_order', 'desc')
            ->where($where)
            ->paginate(3);
        return $this->fetch('index',[
            'list'=>$list
        ]);
    }

    public function add()
    {

        return $this->fetch();
    }

    public function addPost()
    {

        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1, true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        $validate = new NewsVaildate();
        $result = $validate->scene('add')->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }

        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');
        if (NewsModel::create($data)) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $list = NewsModel::find($id);
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost(){
        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        $validate = new NewsVaildate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        $list = NewsModel::find($data['id']);
        if(!$list){
            $this->error('未查询的记录');
        }
        $list->update_time = date('Y-m-d H:i:s');
        if($list->update($data)){
            $this->success('保存成功','News/index');
        }else{
            $this->error('保存失败');
        }
    }

    public function del()
    {
        $id = input('id/d');
        $news = NewsModel::destroy($id);
        if ($news) {
            $this->success('删除成功','News/index');
        } else {
            $this->success('删除失败');
        }
    }
    public function order()
    {
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }
    public function detail()
    {
        $list = input('id/d');
        $list = NewsModel::find($list);
        $this->assign('list',$list);
        return  $this->fetch();
    }
    public function changeStatus()
    {
        $id = input('id/d');
        $list = NewsModel::find($id);
        if ($list['switch'] == 0) {
            $listStatus = 1;
        } else {
            $listStatus = 0;
        }
        $list->switch = $listStatus;
        if ($list->save()) {
            $this->success('修改成功');
        } else {
            $this->success('修改失败');
        }
    }


    }

